﻿using LibNiconico.NiconicoApi.Extentions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Alert
{
    /// <summary>
    /// 認証済みニコニコアラートステータス
    /// </summary>
    public class NicoAlertAuthenticatedStatus:NicoAlertStatus
    {

        /// <summary>
        /// ユーザ名
        /// </summary>
        public string UserName { get; private set; }

        /// <summary>
        /// ユーザ年齢
        /// </summary>
        public int UserAge { get; private set; }

        /// <summary>
        /// 都道府県
        /// </summary>
        public string UserPrefecture { get; private set; }

        /// <summary>
        /// 性別
        /// </summary>
        public string UserSex { get; private set; }

        /// <summary>
        /// プレミアム判定
        /// </summary>
        public bool IsPremium { get; private set; }

        private ICollection<string> communities;

        /// <summary>
        /// コミュニティリスト
        /// </summary>
        public IEnumerable<string> Communities
        {
            get
            {
                return this.communities;
            }
        }

        internal NicoAlertAuthenticatedStatus()
        {
            this.UserName = "";
            this.UserAge = -1;
            this.UserPrefecture = "";
            this.UserSex = "";
            this.IsPremium = false;
            this.communities = new List<string>();
        }

        internal static new NicoAlertAuthenticatedStatus Parse(XDocument document)
        {
            var status = new NicoAlertAuthenticatedStatus();

            Parse(status, document);

            var element = document.Element("getalertstatus");

            if (element != null)
            {
                status.UserName = element.GetElementAsString("user_name");
                status.UserPrefecture = element.GetElementAsString("user_prefecture");
                status.UserAge = element.GetElementAsInt("user_age");
                status.UserSex = element.GetElementAsString("user_sex");
                status.IsPremium = element.GetElementAsBool("is_premium");

                var communities = element.Element("communities");

                if (communities != null)
                {
                    var communityIds = communities.Elements("community_id");

                    foreach (var communityId in communityIds)
                    {
                        status.communities.Add(communityId.Value);
                    }
                }
            }

            return status;
            
        }
    }
}
